package com.zhanbo.zhanbowymusic.main.featured;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.bean.SingerListBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.MLog;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentSingerListBinding;
import com.mwx.baselib.base.BaseFragment;
import com.zhanbo.zhanbowymusic.main.bean.SingerListDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.featured.adapter.SingerListAdapter;
import com.zhanbo.zhanbowymusic.main.featured.adapter.SingerZiMuListAdapter;
import com.zhanbo.zhanbowymusic.main.featured.bean.SingerZiMuListBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 歌手列表
 */
public class SingerListFragment extends BaseFragment<FragmentSingerListBinding> implements BaseBindingAdapter.OnItemClickListener<SingerListBean.ItemsDTO> {
    private String[] alphabet = {"热门", "|", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"
            , "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
    private SingerZiMuListAdapter singerZiMuListAdapter;

    private List<String> alphabetList;
    private ArrayList<SingerZiMuListBean> singerZiMuListBeanList = new ArrayList<>();
    private String areaId="";
    private String type="";


    @Override
    protected Object getLayout() {
        MLog.e("R.layout.fragment_singer_list");

        return R.layout.fragment_singer_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {

        } else {
            // Fragment变为用户不可见时的处理逻辑
        }
        super.setUserVisibleHint(isVisibleToUser);
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


    }
    List<SingerListBean.ItemsDTO> singerList = new ArrayList<>();
    @Override
    protected void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("areaId", areaId);
        map.put("type", type);
//        map.put("keyword", "吴");
        Log.i("歌手列表", map.toString());

        NetworkManager.getInstance().getApiService().getSongerList(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<SingerListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<SingerListBean> listBaseResponses) {
                        MLog.e("歌手列表", listBaseResponses.getData().getItems().toString());
//                        SPUtils.put(SPUtils.TOKEN,listBaseResponses.getData().getToken()+"");
                        singerList = listBaseResponses.getData().getItems();

                        if (singerListAdapter != null) {
                            singerListAdapter.getItems().clear();
                            singerListAdapter.getItems().addAll(listBaseResponses.getData().getItems());
                            binding.rvSingerList.setAdapter(singerListAdapter);
                            if (singerListAdapter.getItems().size()<1){
                                binding.rvSingerList.setVisibility(View.GONE);
                                binding.tvNoData.setVisibility(View.VISIBLE);
                            }else {
                                binding.tvNoData.setVisibility(View.GONE);
                                binding.rvSingerList.setVisibility(View.VISIBLE);
                            }
                        }

                    }
                });

    }

    @Override
    protected void initView() {
        alphabetList = Arrays.asList(alphabet);

        SingerZiMuListBean singerZiMuListBean = new SingerZiMuListBean("热门", true);
        singerZiMuListBeanList.add(singerZiMuListBean);
        for (int i = 1; i < alphabet.length; i++) {
            singerZiMuListBeanList.add(new SingerZiMuListBean(alphabet[i], false));

        }
        binding.rvAlphabet.setLayoutManager(new LinearLayoutManager(_mActivity, LinearLayoutManager.HORIZONTAL, false));
        singerZiMuListAdapter = new SingerZiMuListAdapter(_mActivity);

        singerZiMuListAdapter.getItems().addAll(singerZiMuListBeanList);
        binding.rvAlphabet.setAdapter(singerZiMuListAdapter);
        // 创建我们的ItemDecoration实例
        binding.rvSingerList.setLayoutManager(new GridLayoutManager(_mActivity, 4));
        singerListAdapter = new SingerListAdapter(_mActivity);
        singerListAdapter.setOnItemClickListener(this);
    }

    SingerListAdapter singerListAdapter;

    @Override
    protected void initListener() {
        singerZiMuListAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<SingerZiMuListBean>() {
            @Override
            public void onItemClick(int position) {
                for (SingerZiMuListBean singerZiMuListBean : singerZiMuListBeanList) {
                    singerZiMuListBean.setCheck(false);
                }
                singerZiMuListBeanList.get(position).setCheck(true);
                singerZiMuListAdapter.notifyDataSetChanged();

            }
        });
        binding.rgWord.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                   case  R.id.rb_all_language:
                        areaId = "";
                    break;
                    case  R.id.rb_all_chinese:
                        areaId = "1";
                    break;
                   case  R.id.rb_all_eanda:
                        areaId = "2";
                    break;
                   case  R.id.rb_all_japanese:
                        areaId = "3";
                    break;
                   case  R.id.rb_all_korean:
                        areaId = "4";
                    break;
                   case  R.id.rb_all_other:
                        areaId = "5";
                    break;
                }
                initData();
            }
        });
        binding.rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_all_singer:
                        type="";
                        break;
                    case R.id.rb_man:
                        type="1";
                        break;
                    case R.id.rb_woman:
                        type="2";
                        break;
                    case R.id.rb_group:
                        type="3";
                        break;
                }
                initData();
            }
        });
    }

    public static SingerListFragment newInstance() {

        Bundle args = new Bundle();

        SingerListFragment fragment = new SingerListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 点击歌手列表
     * @param position
     */
    @Override
    public void onItemClick(int position) {
        EventBus.getDefault().post(new SingerListDetailsEBBean(singerListAdapter.getItems().get(position)));
    }


}